package com.lulin.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lulin.mall.product.entity.BrandEntity;
import com.lulin.mall.product.entity.CategoryEntity;
import com.lulin.mall.product.service.BrandService;
import com.lulin.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.Query;

import com.lulin.mall.product.dao.CategoryBrandRelationDao;
import com.lulin.mall.product.entity.CategoryBrandRelationEntity;
import com.lulin.mall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    CategoryService categoryService;
    @Autowired
    BrandService brandService;

    @Autowired
    CategoryBrandRelationDao relationDao;

    /**
     * 保存品牌关联三级类目
     *
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //先根据品牌id、类别id来查询名称
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        BrandEntity brandEntity = brandService.getById(brandId);
        //更新
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    /**
     * 根据品牌id，同步更新类目和品牌关系表的品牌名称
     *
     * @param brandId 品牌id
     * @param name    品牌名称
     */
    @Transactional
    @Override
    public void updateBrandName(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        //根据品牌id，更新品牌名称
        this.update(entity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    /**
     * 根据类别id，更新关联表中的类目名称
     *
     * @param catelogId   类目id
     * @param catelogName 类目名称
     */
    @Transactional
    @Override
    public void updateCatelogName(Long catelogId, String catelogName) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogId(catelogId);
        entity.setCatelogName(catelogName);
        //根据类目id，更新类目名称
        this.update(entity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catelogId));
    }

    /**
     * 根据类目id，查询对应的所有品牌信息
     *
     * @param catId 类目id
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> categoryBranRelation(Long catId) {
        List<CategoryBrandRelationEntity> list = relationDao.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("catelog_id", catId));
        return list;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }


}
